<?php

/**
 * 交易数据
 * Class stat_transdata
 */
class Stat_transdataApp extends BackendApp
{
    /**
     * 默认首页
     */
    function index()
    {
        // 后台登录信息
        $this->assign('admin', $this->visitor->get());

        // 取出订单相关信息
        // 订单模块
        $order_mod = &m('order');
        // 下单人数
        $sum_buyerid = $order_mod->getOne("select count(*) from (select buyer_id,count(buyer_id) as count from {$order_mod->table} group by buyer_id) as a");
        // 付款人数
        $payment_buyerid = $order_mod->getOne("SELECT count(*) from (SELECT buyer_id,count(buyer_id) FROM {$order_mod->table} WHERE status = '" . ORDER_ACCEPTED . "' OR status = '" . ORDER_SHIPPED . "' OR status = '" . ORDER_FINISHED . "' group by buyer_id) as a");

        // 1、下单笔数（所有订单数量）
        $order_number = $order_mod->getOne("SELECT count(*) as allnum FROM {$order_mod->table}");
        $this->assign('order_number', $order_number);

        // 2、付款笔数（状态值为20-已付款，30-已发货，40-已完成共3种情况）
        $payment_number = $order_mod->getOne("SELECT count(*) as allnum FROM {$order_mod->table} WHERE status = '" . ORDER_ACCEPTED . "' OR status = '" . ORDER_SHIPPED . "' OR status = '" . ORDER_FINISHED . "'");
        $this->assign('payment_number', $payment_number);

        // 3、下单金额（所有订单的金额，用的是order_amount字段统计）
        $order_total_amount = $order_mod->getOne("SELECT sum(order_amount) as allsum FROM {$order_mod->table}");
        $this->assign('order_total_amount', $order_total_amount);

        // 4、付款金额（状态值为20-已付款，30-已发货，40-已完成共3种情况的order_amount字段统计之和）
        $payment_amount = $order_mod->getOne("SELECT sum(order_amount) as allnum FROM {$order_mod->table} WHERE status = '" . ORDER_ACCEPTED . "' OR status = '" . ORDER_SHIPPED . "' OR status = '" . ORDER_FINISHED . "'");
        $this->assign('payment_amount', $payment_amount);

        // 5、复购人数
        $repeat_purchase = $order_mod->getOne("select count(*) from (select buyer_id,count(buyer_id) as count from {$order_mod->table} group by buyer_id having count > 1) as a");
        $this->assign('repeat_purchase', $repeat_purchase);

        // 6、订单地域分布，要用图标展现，通过ajax实现

        // 7、转化率（计算公式：付款笔数/订单笔数）
        $conversion_rate = 100 * round($payment_number / $order_number, 2) . '%';
        $this->assign('conversion_rate', $conversion_rate);

        // 8、复购率（指用户购买次数大于1次的次数占比总用户购买次数）
        $repeat_purchase_rate = 100 * round($repeat_purchase / $sum_buyerid, 2) . '%';
        $this->assign('repeat_purchase_rate', $repeat_purchase_rate);

        // 9、支付率（计算公式：付款人数/下单人数）
        $payment_rate = 100 * round($payment_buyerid / $sum_buyerid, 2) . '%';
        $this->assign('payment_rate', $payment_rate);

        // 10、支付金额
        $amount_of_payment = $order_mod->getOne("SELECT sum(order_amount) as allnum FROM {$order_mod->table} WHERE status = '" . ORDER_ACCEPTED . "' OR status = '" . ORDER_SHIPPED . "' OR status = '" . ORDER_FINISHED . "'");
        $this->assign('amount_of_payment', $amount_of_payment);

        // 11、支付方式
        // 支付宝支付订单统计
        $sum_alipay_app_number = $order_mod->getOne("SELECT count(*) as count FROM {$order_mod->table} WHERE status = '" . ORDER_ACCEPTED . "' OR status = '" . ORDER_SHIPPED . "' OR status = '" . ORDER_FINISHED . "' group by payment_code having payment_code = 'alipay_app'");
        // 微信支付订单统计
        $sum_wxpay_app_number = $order_mod->getOne("SELECT count(*) as count FROM {$order_mod->table} WHERE status = '" . ORDER_ACCEPTED . "' OR status = '" . ORDER_SHIPPED . "' OR status = '" . ORDER_FINISHED . "' group by payment_code having payment_code = 'wxpay_app'");
        $this->assign('sum_alipay_app_number', $sum_alipay_app_number);
        $this->assign('sum_wxpay_app_number', $sum_wxpay_app_number);

        // 调用模板
        $this->display('stat_transdata.index.html');
    }

    /**
     * 获取收货人统计信息
     * @return false|string
     */
    function getregionstatics()
    {
        ini_set('display_errors','On');
        error_reporting(E_ALL);
        // 逻辑
        $orderextm_mod = &m('orderextm');
        $region_mod = &m('region');
        $db = &db();
        $consignees = $db->getAll("select count(distinct(consignee)) as sum_consignee,region_id from {$orderextm_mod->table} group by region_id");

        // 遍历取出所有地区的顶级所属
        foreach ($consignees as $k => $consignee) {
            $consignees[$k]['top_region_id'] = $region_mod->getTop($consignee['region_id']);
        }

        // 汇总
        $datas = [];
        foreach ($consignees as $consignee) {
            if (!isset($datas[$consignee['top_region_id']])) {
                $datas[$consignee['top_region_id']] = [
                    'value' => $consignee['sum_consignee'],
                    'name' => $region_mod->get($consignee['top_region_id'])['region_name'],
                ];
            } else {
                // 存在则累计
                $datas[$consignee['top_region_id']]['value'] += $consignee['sum_consignee'];
            }
        }

        // 返回
        $datas = array_values($datas);
        echo json_encode($datas, JSON_UNESCAPED_UNICODE);
    }
}
